package com.szh.zx.duoxiancheng.chapter1;

public class AssemblePoint {
    private int n;

    public AssemblePoint(int n) {
        this.n = n;
    }

    public synchronized void await() throws InterruptedException {
        if (n > 0) {
            n--;
            if (n == 0) {
                notifyAll();
            } else {
                while (n != 0) {
                    wait();
                }
            }
        }
    }


    static class AssemblePointDemo {
        static class Tourist extends Thread {
            AssemblePoint ap;

            public Tourist(AssemblePoint ap) {
                this.ap = ap;
            }

            @Override
            public void run() {
                try {
                    // 先模拟各自独立运行
                    Thread.sleep((int)(Math.random()*1_000));
                    //集合
                    ap.await();
                    System.out.println(Thread.currentThread().getName()+"已到达");
                    // 集合之后的其他操作
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


        public static void main(String[] args) {
            int num=10;
            Tourist[] threads = new Tourist[num];
            AssemblePoint ap = new AssemblePoint(num);
            for (int i = 0; i < num; i++) {
                threads[i] = new Tourist(ap);
                threads[i].start();
            }
        }
    }
}
